/*
 * Copyright (c) TD-Tech Technologies Co., Ltd. 2024-2024. All rights reserved.
 */
export class TimeUtils {
  private static readonly TIME = 9;
  public static readonly NORMAL_FORMAT: number = 2;
  public static readonly BASE_FORMAT: number = 1;
  public static readonly DEFAULT_FORMAT: number = 0;
  public static readonly NO_MILL_TIME_FORMAT = 3;
  public static readonly FILE_TIME_FORMAT: number = 4;
  private static readonly DEFAULT_LENGTH: number = 2;
  private static readonly MILLI_SECOND = 1000;
  static SECOND_ONE_MINUTE: number = 60;
  static SECOND_ONE_HOUR: number = TimeUtils.SECOND_ONE_MINUTE * 60;
  static SECOND_ONE_DAY: number = TimeUtils.SECOND_ONE_HOUR * 24;

  public static getSecondToMillis(time: string): number {
    let date = time.split('T');
    let dateTime = date[0].split('-');
    let timeStr = date[1].split('.');
    let secondTime = timeStr[0].split(':');
    let current = new Date(Number(dateTime[0]), Number(dateTime[1]) - 1, Number(dateTime[this.DEFAULT_LENGTH]),
      Number(secondTime[0]), Number(secondTime[1]), Number(secondTime[this.DEFAULT_LENGTH]));
    return current.getTime();
  }

  public static getTimeMills(): number {
    return new Date().getTime();
  }

  public static getTimeSeconds(): number {
    return Math.round(this.getTimeMills() / 1000);
  }

  public static getCreatedDate(): string {
    return this.getBaseTime(this.NORMAL_FORMAT);
  }

  public static getRequestTime(): number {
    return Number(this.getBaseTime(this.BASE_FORMAT));
  }

  public static getCurrentTime(): string {
    return this.getBaseTime(this.DEFAULT_FORMAT);
  }

  public static getNoMillTime(): string {
    return this.getBaseTime(this.NO_MILL_TIME_FORMAT);
  }

  public static getFileTime(): number {
    return Number(this.getBaseTime(this.FILE_TIME_FORMAT));
  }

  private static getBaseTime(format: number): string {
    let curDate = new Date();
    return this.getTime(curDate, format);
  }

  private static getTime(curDate: Date, format: number): string {
    let year = curDate.getFullYear();
    let month = this.formatTime(curDate.getMonth() + 1);
    let day = this.formatTime(curDate.getDate());
    let hour = this.formatTime(curDate.getHours());
    let minute = this.formatTime(curDate.getMinutes());
    let second = this.formatTime(curDate.getSeconds());
    let milliseconds = curDate.getMilliseconds();

    if (this.BASE_FORMAT === format) {
      return year + '' + month + '' + day + '' + hour + '' + minute + '' + second + '' + milliseconds;
    } else if (this.NORMAL_FORMAT === format) {
      return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '.' + milliseconds;
    } else if (this.NO_MILL_TIME_FORMAT === format) {
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    } else if (this.FILE_TIME_FORMAT === format) {
      return year + '' + month + '' + day + '' + hour + '' + minute + '' + second;
    }

    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milliseconds;
  }

  public static formatTimeByTime(time: number, format: number): string {
    let date = new Date();
    date.setTime(time);
    return this.getTime(date, format);
  }

  private static formatTime(time: number): string | number {
    return time > this.TIME ? time : '0' + time;
  }

  /**

   *
   * @param time 2100-01-01T00:00:00.000+0800
   * @return 2100-01-01 00:00:00
   */
  public static getUserShowTime(time: string): string {
    if (time === null || time === undefined || time === '') {
      return '';
    }
    return time.substr(0, time.indexOf('.')).replace('T', ' ');
  }

  public static getLastNTime(day: number): number {
    let curDate = new Date();
    curDate.setDate(curDate.getDate() - day);
    return Math.floor(curDate.getTime() / this.MILLI_SECOND);
  }

  public static formatSecondToHHmmss(seconds: number): string {
    let hour = Math.floor(seconds / this.SECOND_ONE_HOUR);
    let hourStr = TimeUtils.formatTime(hour);
    seconds -= this.SECOND_ONE_HOUR * hour;
    let min = Math.floor(seconds / this.SECOND_ONE_MINUTE);
    let minStr = TimeUtils.formatTime(min);
    seconds -= this.SECOND_ONE_MINUTE * min;
    let secStr = TimeUtils.formatTime(Math.floor(seconds));
    if(hour == 0){
      return minStr + ':' + secStr;
    }
    return hourStr + ':' + minStr + ':' + secStr;
  }
}